package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.decoration.UserMammonOfflineAdd;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserMammonOfflineAddDaoImpl extends TempDataObjectDao<UserMammonOfflineAdd> {


    public static String getUserMammonOfflineAddSql = "select * from `t_u_mammon_offline_add`";
    public Map<Long, UserMammonOfflineAdd> getUserMammonOfflineAdd() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, UserMammonOfflineAdd> resultMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserMammonOfflineAddSql);
            rs = call.executeQuery();
            while (rs.next()) {
                UserMammonOfflineAdd data = parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return resultMap;
    }

    public static String updateUserMammonOfflineAddSql = "update `t_u_mammon_offline_add` set countMap=? where userId=?";
    @Override
    protected boolean updateData(UserMammonOfflineAdd userMammonOfflineAdd) {
        userMammonOfflineAdd.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userMammonOfflineAdd.getCountMap())));
        para.put(i++, new DbParameter(Types.BIGINT, userMammonOfflineAdd.getUserId()));
        if (getDb().executeNoneQuery(updateUserMammonOfflineAddSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserMammonOfflineAddSql = "insert into `t_u_mammon_offline_add`(userId,countMap) values (?,?)";
    @Override
    protected boolean addData(UserMammonOfflineAdd userMammonOfflineAdd) {
        userMammonOfflineAdd.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userMammonOfflineAdd.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userMammonOfflineAdd.getCountMap())));
        if (getDb().executeNoneQuery(insertUserMammonOfflineAddSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserMammonOfflineAdd parseData(ResultSet rs) throws SQLException {
        UserMammonOfflineAdd data = new UserMammonOfflineAdd();
        data.setUserId(rs.getLong("userId"));
        data.setCountMap(JSON.parseObject(rs.getString("countMap"), new TypeReference<Map<Integer, Integer>>() {}));

        return data;
    }
}
